/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seso;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;

/**
 *
 * @author Administrator
 */
public class RCalendar extends JPanel {
    private Calendar calendar;
    private JTable cTable;
    private JTextField yearField;
    private JTextField monthField;
    private JScrollPane dayPane;
    private JPanel monthPanel;
    private JPanel yearPanel;
    private JButton monthUp;
    private JButton monthDown;
    private JButton yearUp;
    private JButton yearDown;
    private JLabel dateFormatLabel;
    private JPanel dateFormatPanel;
    private JPanel myPanel;
    
    private boolean isDaySelected = false;
    private boolean dateChanged = false;

    public RCalendar() {
        InitComponents();
    }

    private void InitComponents() {
        calendar = Calendar.getInstance();
        cTable = new JTable();
        
        cTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        
        //PaintDay();

        yearDown = new JButton();
        yearDown.setBorder(null);
        //yearDown.setMaximumSize(new Dimension(16, 16));
        //yearDown.setMinimumSize(new Dimension(16, 16));
        //yearDown.setPreferredSize(new Dimension(16, 16));
        //yearDown.setSize(new Dimension(16, 16));
        //yearDown.setText("▼");
        yearDown.setText("\u25C4");
        yearDown.setForeground(Color.gray);
        yearDown.setBackground(Color.white);
        yearDown.setFocusable(false);
        yearDown.setFocusPainted(false);
        yearDown.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
                yearDown_actionPerformed(e);
            }
        });
        
        yearUp = new JButton();
        yearUp.setBorder(null);
        //yearUp.setMaximumSize(new Dimension(16, 16));
        //yearUp.setMinimumSize(new Dimension(16, 16));
        //yearUp.setPreferredSize(new Dimension(16, 16));
        //yearUp.setSize(new Dimension(16, 16));
        //yearUp.setText("▲");
        yearUp.setText("\u25BA");
        yearUp.setForeground(Color.gray);
        yearUp.setBackground(Color.white);
        yearUp.setFocusable(false);
        yearUp.setFocusPainted(false);
        yearUp.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
                yearUp_actionPerformed(e);
            }
        });

        //cTable.setSize(100, 100);
        yearField = new JTextField();
        yearField.setBorder(null);
        yearField.setEnabled(false);
        yearField.setBackground(Color.white);
        yearField.setForeground(Color.black);
        //yearField.setText(Integer.toString(calendar.get(Calendar.YEAR)));
        yearField.setHorizontalAlignment(JTextField.CENTER);
        //yearField.setBounds(105, 0, 60, 25);
        yearField.setPreferredSize(new Dimension(40, 25));
        
        yearPanel = new JPanel();
        yearPanel.setLayout(new BorderLayout());
        yearPanel.add(yearField, BorderLayout.CENTER);
        yearPanel.add(yearDown, BorderLayout.WEST);
        yearPanel.add(yearUp, BorderLayout.EAST);
        
        monthDown = new JButton();
        monthDown.setBorder(null);
        monthDown.setText("\u25C4");
        monthDown.setForeground(Color.gray);
        monthDown.setBackground(Color.white);
        monthDown.setFocusable(false);
        monthDown.setFocusPainted(false);
        monthDown.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
                monthDown_actionPerformed(e);
            }
        });
        monthUp = new JButton();
        monthUp.setAlignmentX((float) 0.0);
        monthUp.setActionMap(null);
        monthUp.setBorder(null);
        monthUp.setText("\u25BA");
        monthUp.setForeground(Color.gray);
        monthUp.setBackground(Color.white);
        monthUp.setFocusable(false);
        monthUp.setFocusPainted(false);
        monthUp.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
                monthUp_actionPerformed(e);
            }
        });

        monthField = new JTextField();
        monthField.setBorder(null);
        monthField.setEnabled(false);
        monthField.setBackground(Color.white);
        monthField.setForeground(Color.black);
        //monthField.setFont(new Font("黑体",Font.BOLD,12));
        //monthField.setText(calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH));
        monthField.setHorizontalAlignment(JTextField.CENTER);
        //monthField.setSize(60, 25);
        monthField.setPreferredSize(new Dimension(60, 25));
        
        monthPanel = new JPanel();
        monthPanel.setLayout(new BorderLayout());
        monthPanel.add(monthField, BorderLayout.CENTER);
        monthPanel.add(monthDown, BorderLayout.WEST);
        monthPanel.add(monthUp, BorderLayout.EAST);

        myPanel = new JPanel();
        myPanel.setBackground(Color.white);
        myPanel.setLayout(new BorderLayout());
        myPanel.add(monthPanel, BorderLayout.WEST);
        myPanel.add(yearPanel, BorderLayout.EAST);
        
        //monthField.setBounds(0, 0, 100, 25);

        //cTable.setMaximumSize(new Dimension(150, 150));
        //cTable.setPreferredSize(new Dimension(150, 150));
        //cTable.setBounds(0, 30, 150, 100);
        cTable.setPreferredScrollableViewportSize(new Dimension(150, 100));
        dayPane = new JScrollPane();
        //tabPane.setBackground(Color.white);
        //tabPane.setBounds(0, 30, 150, 100);
        dayPane.setViewportView(cTable);
        
        JLabel dateFormatLabel = new JLabel();
        dateFormatLabel.setPreferredSize(new Dimension(150, 20));
        dateFormatLabel.setText("Data fomat: DDMMYY");
        dateFormatLabel.setHorizontalAlignment(SwingConstants.CENTER);
        dateFormatLabel.setForeground(Color.lightGray);
        dateFormatLabel.setBackground(Color.white);
        JPanel dateFormatPanel = new JPanel();
        dateFormatPanel.setBackground(Color.white);
        dateFormatPanel.add(dateFormatLabel);

        this.setBackground(Color.white);
        this.setLayout(new BorderLayout());
        //this.add(monthField, BorderLayout.WEST);
        //this.add(monthPanel, BorderLayout.WEST);
        //this.add(yearPanel, BorderLayout.EAST);
        this.add(myPanel, BorderLayout.NORTH);
        this.add(dayPane, BorderLayout.CENTER);
        this.add(dateFormatPanel, BorderLayout.SOUTH);
        
        PaintDate();
        
        //this.viewport.setViewPosition(new Point(100, 100));
        //this.viewport.setBounds(30, 30, 200, 150);
        //this.setViewportView(cTable);
        //this.setSize(250, 200);
        //this.setPreferredSize(new Dimension(400, 400));
        //this.setBounds(0, 0, 250, 200);
    }
    
    private void PaintDay() {
        int defaultSelectRow = 0 ;
        int defaultSelectColumn = 0;
        
        Object[][] t = new Object[6][8];
        String[] h = new String[]{"", "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, 1);
        int w = calendar.get(Calendar.WEEK_OF_YEAR);
        int f = calendar.get(Calendar.DAY_OF_WEEK) + 1;
        calendar.set(Calendar.DATE, day);

        int i = 1;
        for (int j = 0; j < 6; j++) {
            for (int k = 0; k < 8; k++) {
                if (i == calendar.get(Calendar.DATE) && !cTable.isCellSelected(j, k)) {
                    defaultSelectRow = j;
                    defaultSelectColumn = k;
                }
                //if (i == calendar.get(Calendar.DATE)) cTable.changeSelection(k, j, false, false);
                if (k == 0) {
                    if (i <= calendar.getActualMaximum(Calendar.DATE)) {
                        t[j][k] = w++;
                    }
                } else {
                    if (i <= calendar.getActualMaximum(Calendar.DATE)) {
                        if (!(j == 0 && k + 1 < f)) {
                            t[j][k] = i++;
                        }
                    }
                }
            }
        }

        cTable.setModel(new javax.swing.table.DefaultTableModel(
                t, h) {
            Class[] types = new Class [] {
                Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class
            };

            boolean[] canEdit = new boolean[]{
                false, false, false, false, false, false, false, false
            };
            
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
            
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });

        cTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        cTable.setCellSelectionEnabled(true);
        cTable.changeSelection(defaultSelectRow, defaultSelectColumn, false, false);

        RCalendarCellRenderer cellRenderer = new RCalendarCellRenderer();
        for (i = 0; i < 8; i++) {
            cTable.getColumnModel().getColumn(i).setResizable(false);
            cTable.getColumnModel().getColumn(i).setCellRenderer(cellRenderer);
        }
        cTable.setGridColor(Color.gray);
        cTable.setShowGrid(false);
        //cTable.setBorder(null);
        //cTable.setShowHorizontalLines(false);
        //cTable.setShowVerticalLines(false);
        //cTable.getTableHeader().setBorder(null);
        cTable.getTableHeader().setReorderingAllowed(false); 
        //cTable.setDragEnabled(false);
        cTable.setSelectionBackground(new Color(128, 128, 128));
        cTable.setSelectionForeground(Color.black);

        //cTable.setDefaultRenderer(null, new RCalendarCellRenderer());
    }
    
    private void PaintMonth() {
        monthField.setText(calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH));
    }
    
    private void PaintYear() {
        yearField.setText(Integer.toString(calendar.get(Calendar.YEAR)));
    }

    private void PaintDate() {
        PaintYear();
        PaintMonth();
        PaintDay();
    }

    private void jTable1MouseClicked(MouseEvent evt) {
        if (cTable.getColumnModel().getSelectionModel().getLeadSelectionIndex() > 0) {
            Object day = cTable.getModel().getValueAt(cTable.getSelectionModel().getLeadSelectionIndex(), cTable.getColumnModel().getSelectionModel().getLeadSelectionIndex());
            if (day != null) {
                calendar.set(Calendar.DATE, Integer.parseInt(day.toString()));
                dateChanged = true;
            }
        }
    }
    
    public String getSelectedDate()
    {
        String date = null;
        //String date = " ";
        
        if (dateChanged) {
            //return Integer.toString(calendar.getActualMaximum(Calendar.DATE));
            String day = Integer.toString(calendar.get(Calendar.DATE));
            String month = Integer.toString(calendar.get(Calendar.MONTH) + 1);
            String year = Integer.toString(calendar.get(Calendar.YEAR));
        
            date = (day.length() > 1 ? day : "0" + day) + (month.length() > 1 ? month : "0" + month) + year.substring(2);
        }
        
        return date;
        //return calendar.get(Calendar.DATE);
    }
    
    //增加年份   
    void yearUp_actionPerformed(ActionEvent e) {
        calendar.roll(Calendar.YEAR, true);
        dateChanged = true;
        PaintDate();
        //yearField.setText(Integer.toString(calendar.get(Calendar.YEAR)));
        //PaintDay();

        /*year++;
        showYear();
        showDate();
        showDays();*/
    }

//减少年份   
    void yearDown_actionPerformed(ActionEvent e) {
        calendar.roll(Calendar.YEAR, false);
        dateChanged = true;
        PaintDate();
        //yearField.setText(Integer.toString(calendar.get(Calendar.YEAR)));
        //PaintDay();
        /*year--;
        showYear();
        showDate();
        showDays();*/
    }

//减少月份   
    void monthDown_actionPerformed(ActionEvent e) {
        calendar.roll(Calendar.MONTH, false);
        dateChanged = true;
        PaintDate();
        //monthField.setText(calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH));
        //PaintDay();
        /*month--;
        if (month < 0) {
            month = 11;
            year--;
            showYear();
        }
        showMonth();
        showDate();
        showDays();*/
    }

//增加月份   
    void monthUp_actionPerformed(ActionEvent e) {
        calendar.roll(Calendar.MONTH, true);
        dateChanged = true;
        PaintDate();
        //monthField.setText(calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH));
        //PaintDay();
        /*month++;
        if (month == 12) {
            month = 0;
            year++;
            showYear();
        }
        showMonth();
        showDate();
        showDays();*/
    }
    
    public Calendar getCalendar() {
        return calendar;
    }

    public void setCalendar(Calendar calendar) {
        this.calendar = calendar;
        dateChanged = true;
        PaintDate();
    }

    public boolean isDateChanged() {
        return dateChanged;
    }
}
